package com.example.library_common.db;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.example.library_common.R;
import com.example.library_common.global.AppGlobals;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class GoodsDetailMsBean implements Serializable {


    /**
     * goodsPics : {"pics":[{"url":"http://162.247.99.201:81/shop/shop_top.jpg"},{"url":"http://162.247.99.201:81/shop/shop_top.jpg"},{"url":"http://162.247.99.201:81/shop/shop_top.jpg"}]}
     * gid : 12
     * goodsColor : {"color":[{"name":"蓝色"},{"name":"黑红色"}]}
     * goodsSize : {"size":[{"name":"L"},{"name":"XL"}]}
     * isCollect : 0
     * updateTime : 2020-09-18 16:11:03
     * goodsOldPrice : 100
     * goodsContent : {"pics":[{"url":"http://162.247.99.201:81/shop/shop_top.jpg"},{"url":"http://162.247.99.201:81/shop/shop_top.jpg"},{"url":"http://162.247.99.201:81/shop/shop_top.jpg"}]}
     * goodsStock : 97
     * goodsSales : 3
     * catId : 1,7,11,12,13
     * goodsStatus : 1
     * createTime : 2020-09-18 16:11:03
     * goodsPrice : 73
     * goodsName : 俗世堂海参牡蛎复合肽
     * goodsFare : 0
     */


    @JSONField(name = "goodsPics")
    private String goodsPics;

    @JSONField(name = "gid")
    private int gid;

    @JSONField(name = "goodsColor")
    private String goodsColor;

    @JSONField(name = "goodsSize")
    private String goodsSize;

    @JSONField(name = "isCollect")
    private int isCollect;

    @JSONField(name = "updateTime")
    private String updateTime;

    @JSONField(name = "goodsOldPrice")
    private int goodsOldPrice;

    @JSONField(name = "goodsContent")
    private String goodsContent;

    @JSONField(name = "goodsStock")
    private int goodsStock;

    @JSONField(name = "goodsSales")
    private int goodsSales;

    @JSONField(name = "catId")
    private String catId;

    @JSONField(name = "goodsStatus")
    private int goodsStatus;

    @JSONField(name = "createTime")
    private String createTime;

    @JSONField(name = "goodsPrice")
    private int goodsPrice;

    @JSONField(name = "goodsName")
    private String goodsName;

    @JSONField(name = "goodsFare")
    private int goodsFare;

    @JSONField(name = "givenCml")
    private double givenCml;

    public double getGivenCml() {
        return givenCml;
    }

    public void setGivenCml(double givenCml) {
        this.givenCml = givenCml;
    }

    private List<String> goodsColorLs = new ArrayList<>();

    private List<String> goodsSizeLs = new ArrayList<>();

    private String selectedColorLabs = "";

    private String selectedSizeLabs = "";

    private int selectedCount = 1;

    private String selectedGoodsType = "";

    public String getSelectedGoodsType() {
        return selectedGoodsType;
    }

    public void setSelectedGoodsType(String selectedGoodsType) {
        this.selectedGoodsType = selectedGoodsType;
    }

    public int getSelectedCount() {
        return selectedCount;
    }

    public String getSelectedCountStr() {
        return selectedCount + "";
    }

    public void setSelectedCount(int selectedCount) {
        this.selectedCount = selectedCount;
    }

    public String getSelectedColorLabs() {
        return selectedColorLabs;
    }

    public String getSelectedSizeLabs() {
        return selectedSizeLabs;
    }

    public void addSelectedSizeLab(String lab) {

        if (TextUtils.isEmpty(lab)) return;

        selectedSizeLabs = lab;
    }

    public void addSelectedColorLab(String lab) {

        if (TextUtils.isEmpty(lab)) return;

        selectedColorLabs = lab;


    }

    public String defaultSelectSizeLab() {

        if (!TextUtils.isEmpty(selectedSizeLabs)) {
            return selectedSizeLabs;
        }

        goodsSizeLs = getGoodsSizeLs();
        if (!goodsSizeLs.isEmpty()) {
            return goodsSizeLs.get(0);
        }
        return "";
    }

    public String defaultSelectColorLab() {
        if (!TextUtils.isEmpty(selectedColorLabs)) {
            return selectedColorLabs;
        }
        goodsColorLs = getGoodsColorLs();
        if (!goodsColorLs.isEmpty()) {
            return goodsColorLs.get(0);
        }
        return "";
    }

    public String getSelectedLabStr() {
        String str = (!TextUtils.isEmpty(selectedColorLabs) ?
                selectedColorLabs : defaultSelectColorLab())
                + "," +
                (!TextUtils.isEmpty(selectedSizeLabs) ?
                        selectedSizeLabs : defaultSelectSizeLab());
        return selectedGoodsType.isEmpty() ? str : selectedGoodsType;
    }


    public List<String> getGoodsColorLs() {
        if (goodsColorLs.isEmpty()) {

            Object object = JSON.parse(goodsColor);

            if (object instanceof JSONObject) {

                Object colorObject = ((JSONObject) object).get("color");

                if (colorObject instanceof JSONArray) {

                    JSONArray colorArray = (JSONArray) colorObject;

                    for (Object next : colorArray) {

                        if (next instanceof JSONObject) {

                            String url = ((JSONObject) next).getString("name");

                            goodsColorLs.add(url);
                        }
                    }
                }
            }
        }
        return goodsColorLs;
    }

    public List<String> getGoodsSizeLs() {

        if (goodsSizeLs.isEmpty()) {

            Object object = JSON.parse(goodsSize);

            if (object instanceof JSONObject) {

                Object colorObject = ((JSONObject) object).get("size");

                if (colorObject instanceof JSONArray) {

                    JSONArray colorArray = (JSONArray) colorObject;

                    for (Object next : colorArray) {

                        if (next instanceof JSONObject) {

                            String url = ((JSONObject) next).getString("name");

                            goodsSizeLs.add(url);
                        }
                    }

                }
            }
        }
        return goodsSizeLs;
    }

    private String defaultImg = "";

    public String getDefaultImg() {
        if (TextUtils.isEmpty(defaultImg)) {
            defaultImg = parseGoodsPics();
        }
        return defaultImg;
    }

    public String parseGoodsPics() {

        String picStr = "";

        if (TextUtils.isEmpty(goodsPics)) return picStr;

        JSONObject mPics = JSON.parseObject(goodsPics);

        Object pics = mPics.get("pics");

        if (pics == null) return picStr;

        JSONArray array = JSON.parseArray(pics.toString());

        if (array.size() > 0) {

            Object o = array.get(0);

            if (o instanceof JSONObject) {

                picStr = ((JSONObject) o).getString("url");
            }
        }
        return picStr;
    }

    public String getGoodsPics() {
        return goodsPics;
    }

    public void setGoodsPics(String goodsPics) {
        this.goodsPics = goodsPics;
    }

    public int getGid() {
        return gid;
    }

    public void setGid(int gid) {
        this.gid = gid;
    }

    public String getGoodsColor() {
        return goodsColor;
    }

    public void setGoodsColor(String goodsColor) {
        this.goodsColor = goodsColor;
    }

    public String getGoodsSize() {
        return goodsSize;
    }

    public void setGoodsSize(String goodsSize) {
        this.goodsSize = goodsSize;
    }

    public int getIsCollect() {
        return isCollect;
    }

    public void setIsCollect(int isCollect) {
        this.isCollect = isCollect;
    }

    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }

    public int getGoodsOldPrice() {
        return goodsOldPrice;
    }

    public void setGoodsOldPrice(int goodsOldPrice) {
        this.goodsOldPrice = goodsOldPrice;
    }

    public String getGoodsContent() {
        return goodsContent;
    }

    public void setGoodsContent(String goodsContent) {
        this.goodsContent = goodsContent;
    }

    public int getGoodsStock() {
        return goodsStock;
    }

    public void setGoodsStock(int goodsStock) {
        this.goodsStock = goodsStock;
    }

    public int getGoodsSales() {
        return goodsSales;
    }

    public void setGoodsSales(int goodsSales) {
        this.goodsSales = goodsSales;
    }

    public String getCatId() {
        return catId;
    }

    public void setCatId(String catId) {
        this.catId = catId;
    }

    public int getGoodsStatus() {
        return goodsStatus;
    }

    public void setGoodsStatus(int goodsStatus) {
        this.goodsStatus = goodsStatus;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public int getGoodsPrice() {
        return goodsPrice;
    }

    public void setGoodsPrice(int goodsPrice) {
        this.goodsPrice = goodsPrice;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public int getGoodsFare() {
        return goodsFare;
    }

    public String getGoodsFareStr() {
        if (goodsFare <= 0) {
            return "免运费";
        }
        return String.valueOf(goodsFare);
    }

    public void setGoodsFare(int goodsFare) {
        this.goodsFare = goodsFare;
    }


    public boolean isCollected() {
        return isCollect == 1;
    }

    public void onCollecting() {
        isCollect = 1;
    }

    public void onCancelCollecting() {
        isCollect = 0;
    }

    public int toggleCollecting() {
        isCollect = isCollect == 0 ? 1 : 0;
        return isCollect;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        GoodsDetailMsBean that = (GoodsDetailMsBean) o;
        return gid == that.gid
                && defaultSelectColorLab().equals(that.defaultSelectColorLab())
                && defaultSelectSizeLab().equals(that.defaultSelectSizeLab());
    }


}
